home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP08.ZIP / CHAP08 / PATRON / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  23KB  |  1,031 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Modifications for Chapter 8
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <memory.h>
  19. #include "patron.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * CPatronDoc::CPatronDoc
  25.  * CPatronDoc::~CPatronDoc
  26.  *
  27.  * Constructor Parameters:
  28.  *  hInst           HINSTANCE of the application.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst)
  32.     : CDocument(hInst)
  33.     {
  34.     m_pPG=NULL;
  35.     m_lVer=VERSIONCURRENT;
  36.     m_pIStorage=NULL;
  37.     m_fPrintSetup=TRUE;
  38.  
  39.     //CHAPTER8MOD
  40.     m_pDropTarget=NULL;
  41.     //End CHAPTER8MOD
  42.     return;
  43.     }
  44.  
  45.  
  46. CPatronDoc::~CPatronDoc(void)
  47.     {
  48.     //CHAPTER8MOD
  49.     if (NULL!=m_pDropTarget)
  50.         {
  51.         RevokeDragDrop(m_hWnd);
  52.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, FALSE, TRUE);
  53.         m_pDropTarget->Release();
  54.         }
  55.     //End CHAPTER8MOD
  56.  
  57.     if (NULL!=m_pPG)
  58.         delete m_pPG;
  59.  
  60.     if (NULL!=m_pIStorage)
  61.         m_pIStorage->Release();
  62.  
  63.     CoFreeUnusedLibraries();
  64.     return;
  65.     }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. /*
  72.  * CPatronDoc::FInit
  73.  *
  74.  * Purpose:
  75.  *  Initializes an already created document window.  The client actually
  76.  *  creates the window for us, then passes that here for further
  77.  *  initialization.
  78.  *
  79.  * Parameters:
  80.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  81.  *
  82.  * Return Value:
  83.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  84.  */
  85.  
  86. BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
  87.     {
  88.     //Change the stringtable range to our customization.
  89.     pDI->idsMin=IDS_DOCUMENTMIN;
  90.     pDI->idsMax=IDS_DOCUMENTMAX;
  91.  
  92.     //Do default initialization
  93.     if (!CDocument::FInit(pDI))
  94.         return FALSE;
  95.  
  96.     //Pages are created when we get a ::ULoad later.
  97.     return TRUE;
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /*
  106.  * CPatronDoc::FMessageHook
  107.  *
  108.  * Purpose:
  109.  *  Processes WM_SIZE for the document so we can resize the Pages window.
  110.  *
  111.  * Parameters:
  112.  *  <WndProc Parameters>
  113.  *  pLRes           LRESULT FAR * in which to store the return value
  114.  *                  for the message.
  115.  *
  116.  * Return Value:
  117.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  118.  */
  119.  
  120. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  121.     , LPARAM lParam, LRESULT FAR *pLRes)
  122.     {
  123.     UINT        dx, dy;
  124.     RECT        rc;
  125.  
  126.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  127.         {
  128.         dx=LOWORD(lParam);
  129.         dy=HIWORD(lParam);
  130.  
  131.         //Resize the Pages window to fit the new client area of the document
  132.         GetClientRect(hWnd, &rc);
  133.         m_pPG->RectSet(&rc, FALSE);
  134.         }
  135.  
  136.     /*
  137.      * We return FALSE even on WM_SIZE so we can let the default procedure
  138.      * handle maximized MDI child windows appropriately.
  139.      */
  140.     return FALSE;
  141.     }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * CPatronDoc::Clear
  152.  *
  153.  * Purpose:
  154.  *  Sets all contents in the document back to defaults with no filename.
  155.  *
  156.  * Paramters:
  157.  *  None
  158.  *
  159.  * Return Value:
  160.  *  None
  161.  */
  162.  
  163. void CPatronDoc::Clear(void)
  164.     {
  165.     //Completely reset the pages
  166.     m_pPG->FIStorageSet(NULL, FALSE, FALSE);
  167.  
  168.     CDocument::Clear();
  169.     m_lVer=VERSIONCURRENT;
  170.     return;
  171.     }
  172.  
  173.  
  174.  
  175.  
  176. /*
  177.  * CPatronDoc::FDirtyGet
  178.  *
  179.  * Purpose:
  180.  *  Returns the current dirty status of the document.
  181.  *
  182.  * Parameters:
  183.  *  None
  184.  *
  185.  * Return Value:
  186.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  187.  */
  188.  
  189. BOOL CPatronDoc::FDirtyGet()
  190.     {
  191.     BOOL    fPageDirty;
  192.  
  193.     fPageDirty=m_pPG->FIsDirty();
  194.     return m_fDirty | fPageDirty;
  195.     }
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /*
  202.  * CPatronDoc::Delete
  203.  *
  204.  * Purpose:
  205.  *  Removed the current object from the document.
  206.  *
  207.  * Paramters:
  208.  *  None
  209.  *
  210.  * Return Value:
  211.  *  None
  212.  */
  213.  
  214. void CPatronDoc::Delete(void)
  215.     {
  216.     if (NULL!=m_pPG)
  217.         m_pPG->TenantDestroy();
  218.  
  219.     CoFreeUnusedLibraries();
  220.     return;
  221.     }
  222.  
  223.  
  224.  
  225. /*
  226.  * CPatronDoc::FQueryPrinterSetup
  227.  *
  228.  * Purpose:
  229.  *  Returns whether or not the Printer Setup menu item can be
  230.  *  enabled.  Once you create a tenant in any page, Printer Setup
  231.  *  is voided simply to keep this sample simple, that is, we don't
  232.  *  have to worry about reorganizing potentially large amounts
  233.  *  of layout after we start plopping down objects.
  234.  *
  235.  * Parameters:
  236.  *  None
  237.  *
  238.  * Return Value:
  239.  *  BOOL            TRUE to enable the menu, FALSE otherwise.
  240.  */
  241.  
  242. BOOL CPatronDoc::FQueryPrinterSetup(void)
  243.     {
  244.     return m_fPrintSetup;
  245.     }
  246.  
  247.  
  248.  
  249.  
  250.  
  251. /*
  252.  * CPatronDoc::FQueryObjectSelected
  253.  *
  254.  * Purpose:
  255.  *  Returns whether or not there is an object selected in this
  256.  *  document for Cut, Copy, Delete functions.
  257.  *
  258.  * Parameters:
  259.  *  hMenu           HMENU of the Edit menu.
  260.  *
  261.  * Return Value:
  262.  *  BOOL            TRUE if we have an object, FALSE otherwise.
  263.  */
  264.  
  265. BOOL CPatronDoc::FQueryObjectSelected(HMENU hMenu)
  266.     {
  267.     return m_pPG->FQueryObjectSelected(hMenu);
  268.     }
  269.  
  270.  
  271.  
  272.  
  273.  
  274. /*
  275.  * CPatronDoc::ULoad
  276.  *
  277.  * Purpose:
  278.  *  Loads a given document without any user interface overwriting the
  279.  *  previous contents of the editor.
  280.  *
  281.  * Parameters:
  282.  *  fChangeFile     BOOL indicating if we're to update the window title
  283.  *                  and the filename from using this file.
  284.  *  pszFile         LPSTR to the filename to load.  Could be NULL for
  285.  *                  an untitled document.
  286.  *
  287.  * Return Value:
  288.  *  UINT            An error value from DOCERR_*
  289.  */
  290.  
  291.  
  292. UINT CPatronDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  293.     {
  294.     RECT        rc;
  295.     LPSTORAGE   pIStorage;
  296.     HRESULT     hr;
  297.     CLSID       clsID;
  298.     DWORD       dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  299.  
  300.     if (NULL==pszFile)
  301.         {
  302.         //Create a new temp file.
  303.         hr=StgCreateDocfile(NULL, dwMode | STGM_CREATE | STGM_DELETEONRELEASE
  304.             , 0, &pIStorage);
  305.  
  306.         //Mark this as one of our class since we check with ReadClassStg below.
  307.         if (SUCCEEDED(hr))
  308.             WriteClassStg(pIStorage, CLSID_PatronPages);
  309.         }
  310.     else
  311.         {
  312.         hr=StgOpenStorage(pszFile, NULL, dwMode, NULL, 0, &pIStorage);
  313.         }
  314.  
  315.     if (FAILED(hr))
  316.         return DOCERR_COULDNOTOPEN;
  317.  
  318.     //Check if this is our type of file and exit if not.
  319.     hr=ReadClassStg(pIStorage, &clsID);
  320.  
  321.     if (FAILED(hr) || !IsEqualCLSID(clsID, CLSID_PatronPages))
  322.         {
  323.         pIStorage->Release();
  324.         return DOCERR_READFAILURE;
  325.         }
  326.  
  327.     //Attempt to create our contained Pages window.
  328.     m_pPG=new CPages(m_hInst, m_cf);
  329.     GetClientRect(m_hWnd, &rc);
  330.  
  331.     if (!m_pPG->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_PAGES, NULL))
  332.         {
  333.         pIStorage->Release();
  334.         return DOCERR_READFAILURE;
  335.         }
  336.  
  337.     if (!m_pPG->FIStorageSet(pIStorage, FALSE, (BOOL)(NULL==pszFile)))
  338.         {
  339.         pIStorage->Release();
  340.         return DOCERR_READFAILURE;
  341.         }
  342.  
  343.     //CHAPTER8MOD
  344.     //Open the window up for drag-drop
  345.     m_pDropTarget=new CDropTarget(this);
  346.  
  347.     if (NULL!=m_pDropTarget)
  348.         {
  349.         m_pDropTarget->AddRef();
  350.         RegisterDragDrop(m_hWnd, (LPDROPTARGET)m_pDropTarget);
  351.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, TRUE, FALSE);
  352.         }
  353.     //End CHAPTER8MOD
  354.  
  355.     m_pIStorage=pIStorage;
  356.     Rename(pszFile);
  357.  
  358.     //Do initial setup if this is a new file, otherwise Pages handles things.
  359.     if (NULL==pszFile)
  360.         {
  361.         //Go initialize the Pages for the default printer.
  362.         if (!PrinterSetup(NULL, TRUE))
  363.             return DOCERR_COULDNOTOPEN;
  364.  
  365.         //Go create an initial page.
  366.         m_pPG->PageInsert(0);
  367.         }
  368.     else
  369.         m_fPrintSetup=FALSE;    //Can't change an already saved configuration
  370.  
  371.     FDirtySet(FALSE);
  372.     return DOCERR_NONE;
  373.     }
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381. /*
  382.  * CPatronDoc::USave
  383.  *
  384.  * Purpose:
  385.  *  Writes the file to a known filename, requiring that the user has
  386.  *  previou